
package cn.com.xy.sms.sdk.util;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Random;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.ui.popu.util.ViewUtil;
 

public class XmlManager {
	
	  public static HashMap<String, String> getBusinessTitle(Document doc, String id) {
	        try {
	        	if(doc==null) return null;
	            NodeList ndList = null;
	            Element tempEl = null;
	            Node node = null;
	            NodeList ndChildList = null;
	            String tagName = null;
	            
	            if (ViewUtil.getChannelType() == 7) {
	                ndList = doc.getFirstChild().getChildNodes();
	            } else {
	                ndList = doc.getElementsByTagName("title_" + id);
	            }

	            if (ndList != null && ndList.getLength() > 0) {
	                int len = ndList.getLength();
	                HashMap<String, String>  imagePathMap =  new HashMap<String, String>();
	                for (int i = 0; i < len; i++) {
	                    tempEl = (Element) ndList.item(i);
	                    ndChildList = tempEl.getElementsByTagName("popu");
	                    if (ndChildList != null && ndChildList.getLength() > 0) {
	                        int popuslen = ndChildList.getLength();

	                        int index = 0;
	                        if (popuslen > 1) {
	                            Random rd = new Random();
	                            index = rd.nextInt(popuslen * 3);
	                            index = index % popuslen;
	                        }

	                        node = ndChildList.item(index);
	                        ndChildList = node.getChildNodes();

	                        for (int k = 0; k < ndChildList.getLength(); k++) {
	                            node = ndChildList.item(k);
	                            tagName = node.getNodeName();
	                            imagePathMap.put(tagName, XmlManager.getTextContent(node));
	                        }

	                    }
	                }
	                return imagePathMap;
	            } else {
	                // LogManager.i("getSmsTitleById", "没找点节点");
	                return null;
	            }
	        } catch (Throwable e) {
	            // TODO: handle Throwable
	        } finally {

	        }
	        return null;
	    }
	  
	  public static HashMap<String, String> getBusinessTitle(Document doc) {
	        try {
	            // LogManager.i("getBankSmsTitleById", "id ="+id);
	            if(doc==null)return null;
	            Node node = null;
	            NodeList ndChildList = null;
	            String tagName = null;
	            
                ndChildList = doc.getElementsByTagName("popu");
                if (ndChildList != null && ndChildList.getLength() > 0) {
                    int popuslen = ndChildList.getLength();
                    HashMap<String, String> imagePathMap = new HashMap<String, String>();
                    int index = 0;
                    if (popuslen > 1) {
                        Random rd = new Random();
                        index = rd.nextInt(popuslen * 3);
                        index = index % popuslen;
                    }

                    node = ndChildList.item(index);
                    ndChildList = node.getChildNodes();

                    for (int k = 0; k < ndChildList.getLength(); k++) {
                        node = ndChildList.item(k);
                        tagName = node.getNodeName();
                         imagePathMap.put(tagName, XmlManager.getTextContent(node));
                    }
                    return imagePathMap;

                }
	            return null;

	        } catch (Throwable e) {
	            // TODO: handle Throwable
	        } finally {

	        }
	        return null;
	    }



//    public static Document getDocument(InputStream is) throws Exception {
//        DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
//        DocumentBuilder db = dbfactory.newDocumentBuilder();
//        return db.parse(is);
//    }

    /**
     * 获取节点的值
     * 
     * @param node
     * @return
     */
    public static String getTextContent(Node node) {
        try {
            if (node != null) {
                // return node.getTextContent();
                Node child = node.getFirstChild();
                if (child != null) {
                    String resString = child.getNodeValue().trim();
                    // LogManager.d("test", "resString: "+resString);
                    return resString;
                }
            }
        } catch (Throwable e) {
            // TODO: handle Throwable
        }

        return "";
    }
    
    public static int getIntContent(String pubId){
    	if(!StringUtils.isNull(pubId))
    	{
    		try {
				return Integer.parseInt(pubId);
			} catch (Throwable e) {
//				e.printStackTrace();
	            LogManager.e(Constant.TAG, "getIntContent: ", e);

			}
    	}
    	return 0;
    	
    }
    
    public static String getContentByTag(Element root,String TagName){
    
    	try {
    		NodeList list = null;
    		list=root.getElementsByTagName(TagName);
    		if(list!=null&&list.getLength()>0){
    			return XmlManager.getTextContent(list.item(0)).toString();			
    		}	
		} catch (Throwable e) {
//			e.printStackTrace();
            LogManager.e(Constant.TAG, "getContentByTag: ", e);

		}
		
		return "";
    }
}
